home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1988 / 07 / bench / benchold.bas < prev    next >
BASIC Source File  |  1988-04-29  |  7KB  |  309 lines

  1. [FILENAME: BENCHOLD.BAS]
  2.  
  3. DEFINT A-Z
  4. DIM t!(28)
  5. OPEN "bas6.tim" FOR OUTPUT AS #1
  6. 'time for a raw integer loop, executed 1,000,000 times.
  7. t! = TIMER
  8. FOR i = 1 TO 1000
  9.   FOR j = 1 TO 1000
  10.   NEXT j
  11. NEXT i
  12. t!(0) = TIMER - t!
  13.  
  14. 'time for a integer assignment loop, executed 1,000,000 times.
  15. y = 5: z = -5
  16. t! = TIMER
  17. FOR i = 1 TO 1000
  18.   FOR j = 1 TO 1000
  19.   x = y
  20.   x = z
  21.   NEXT j
  22. NEXT i
  23. t!(1) = (TIMER - t! - t!(0)) / 2
  24.  
  25. 'time for 1,000,000 integer adds
  26. y = 5: z = -5
  27. t! = TIMER
  28. FOR i = 1 TO 1000
  29.   FOR j = 1 TO 1000
  30.   x = x + y
  31.   x = x + z
  32.   NEXT j
  33. NEXT i
  34. t!(2) = (TIMER - t! - t!(0)) / 2 - t!(1)
  35.  
  36. 'time for 1,000,000 integer subtracts
  37. t! = TIMER
  38. FOR i = 1 TO 1000
  39.   FOR j = 1 TO 1000
  40.   x = x - y
  41.   x = x - z
  42.   NEXT j
  43. NEXT i
  44. t!(3) = (TIMER - t! - t!(0)) / 2 - t!(1)
  45.  
  46. 'time for 1,000,000 integer multiplies
  47. k = 7
  48. t! = TIMER
  49. FOR i = 1 TO 1000
  50.   FOR j = 1 TO 1000
  51.   x = k * j
  52.   NEXT j
  53. NEXT i
  54. t!(4) = TIMER - t! - t!(1)
  55.  
  56. 'time for 1,000,000 integer divides
  57. t! = TIMER
  58. FOR i = 1 TO 1000
  59.   FOR j = 1 TO 1000
  60.   x = i \ j
  61.   NEXT j
  62. NEXT i
  63. t!(5) = TIMER - t! - t!(1)
  64.  
  65. 'time for 100,000 string assignments
  66. t! = TIMER
  67. FOR i = 1 TO 1000
  68.   FOR j = 1 TO 100
  69.   x$ = "abcdefghijklmnopqrstuvwxyz"
  70.   x$ = "zyxwvutsrqponmlkjihgfedcba"
  71.   NEXT j
  72. NEXT i
  73. t!(6) = (TIMER - t! - t!(0) / 10) / 2
  74.  
  75. 'time for 100,000 string MID$ operations
  76. x$ = "abcdefghijklmnopqrstuvwxyz"
  77. k = 17
  78. t! = TIMER
  79. FOR i = 1 TO 1000
  80.   FOR j = 1 TO 100
  81.   MID$(x$, k, 1) = "d"
  82.   NEXT j
  83. NEXT i
  84. t!(7) = TIMER - t! - t!(0) / 10
  85.  
  86. 'time for 10,000 string concatenations
  87. x$ = ""
  88. t! = TIMER
  89. FOR i = 1 TO 10000
  90.    x$ = x$ + "a"
  91. NEXT i
  92. t!(8) = TIMER - t! - t!(6) / 10 - t!(0) / 100
  93.  
  94. 'time for a single-precision assignment loop, executed 1,000,000 times.
  95. y! = 5!: z! = -5!
  96. t! = TIMER
  97. FOR i = 1 TO 1000
  98.   FOR j = 1 TO 1000
  99.   x! = y!
  100.   x! = z!
  101.   NEXT j
  102. NEXT i
  103. t!(9) = (TIMER - t! - t!(0)) / 2
  104.  
  105. 'time for 1,000,000 single-precision adds
  106. t! = TIMER
  107. FOR i = 1 TO 1000
  108.   FOR j = 1 TO 1000
  109.     x! = x! + y!
  110.     x! = x! + z!
  111.   NEXT j
  112. NEXT i
  113. t!(10) = (TIMER - t! - t!(0)) / 2 - t!(9)
  114.  
  115. 'time for 1,000,000 single-precision subtracts
  116. x! = 0!
  117. t! = TIMER
  118. FOR i = 1 TO 1000
  119.   FOR j = 1 TO 1000
  120.     x! = x! - y!
  121.     x! = x! - z!
  122.   NEXT j
  123. NEXT i
  124. t!(11) = (TIMER - t! - t!(0)) / 2 - t!(9)
  125.  
  126. 'time for 100,000 single-precision multiplies
  127. x! = 1!
  128. t! = TIMER
  129. FOR i = 1 TO 1000
  130.   FOR j = 1 TO 100
  131.     x! = x! * 1.00001
  132.   NEXT j
  133. NEXT i
  134. t!(12) = TIMER - t! - t!(0) / 10 - t!(9) / 10
  135.  
  136. 'time for 100,000 single-precision divides
  137. t! = TIMER
  138. FOR i = 1 TO 1000
  139.   FOR j = 1 TO 100
  140.     x! = x! / 1.00001
  141.   NEXT j
  142. NEXT i
  143. t!(13) = TIMER - t! - t!(0) / 10 - t!(9) / 10
  144.  
  145. 'error in single-precision multiply/divide
  146. t!(14) = x! - 1!
  147.  
  148. 'time for 10,000 single-precision exponentiations
  149. x! = 100!
  150. t! = TIMER
  151. FOR i = 1 TO 10000
  152.   x! = x! ^ .999999
  153. NEXT i
  154. t!(15) = TIMER - t! - t!(0) / 100 - t!(9) / 100
  155.  
  156. 'time for a double-precision assignment loop, executed 1,000,000 times.
  157. y# = 5.5#: z# = -5.5#
  158. t! = TIMER
  159. FOR i = 1 TO 1000
  160.   FOR j = 1 TO 1000
  161.   x# = y#
  162.   x# = z#
  163.   NEXT j
  164. NEXT i
  165. t!(16) = (TIMER - t! - t!(0)) / 2
  166.  
  167. 'time for 1,000,000 double-precision adds
  168. t! = TIMER
  169. FOR i = 1 TO 1000
  170.   FOR j = 1 TO 1000
  171.     x# = x# + y#
  172.   NEXT j
  173. NEXT i
  174. t!(17) = TIMER - t! - t!(16) - t!(0)
  175.  
  176. 'time for 1,000,000 double-precision subtracts
  177. x# = 0#
  178. t! = TIMER
  179. FOR i = 1 TO 1000
  180.   FOR j = 1 TO 1000
  181.     x# = x# - y#
  182.   NEXT j
  183. NEXT i
  184. t!(18) = TIMER - t! - t!(16) - t!(0)
  185.  
  186. 'time for 100,000 double-precision multiplies
  187. x# = 1#
  188. t! = TIMER
  189. FOR i = 1 TO 1000
  190.   FOR j = 1 TO 100
  191.     x# = x# * 1.00001#
  192.   NEXT j
  193. NEXT i
  194. t!(19) = (TIMER - t! - t!(0) / 10) - t!(16) / 10
  195.  
  196. 'time for 100,000 double-precision divides
  197. t! = TIMER
  198. FOR i = 1 TO 1000
  199.   FOR j = 1 TO 100
  200.     x# = x# / 1.00001#
  201.   NEXT j
  202. NEXT i
  203. t!(20) = (TIMER - t! - t!(0) / 10) - t!(16) / 10
  204.  
  205. 'error in double-precision multiply/divide
  206. t!(21) = x# - 1#
  207.  
  208. 'time for 10,000 double-precision exponentiations
  209. x# = 100#
  210. t! = TIMER
  211. FOR i = 1 TO 10000
  212.   x# = x# ^ .999999#
  213. NEXT i
  214. t!(22) = (TIMER - t! - t!(0) / 100) - t!(16) / 100
  215.  
  216.  
  217. 'following are logical comparisons and operators
  218.  
  219. 'time for 1,000,000 integer comparisons
  220. x = 0
  221. t! = TIMER
  222. FOR i = 1 TO 1000
  223.   FOR j = 1 TO 1000
  224.     IF i < x THEN x = 1
  225.   NEXT j
  226. NEXT i
  227. t!(23) = TIMER - t! - t!(0)
  228.  
  229. 'time for 1,000,000 single-precision comparisons
  230. x! = 5!: y! = 3.333
  231. t! = TIMER
  232. FOR i = 1 TO 1000
  233.   FOR j = 1 TO 1000
  234.     IF x! < y! THEN x = 1
  235.   NEXT j
  236. NEXT i
  237. t!(24) = TIMER - t! - t!(0)
  238.  
  239. 'time for 1,000,000 double-precision comparisons
  240. x# = 5#: y# = 3.333#
  241. t! = TIMER
  242. FOR i = 1 TO 1000
  243.   FOR j = 1 TO 1000
  244.     IF x# < y# THEN x = 1
  245.   NEXT j
  246. NEXT i
  247. t!(25) = TIMER - t! - t!(0)
  248.  
  249. 'is there short-circuit expression evaluation?
  250. 'integer loop, 1,000,000 times
  251. t! = TIMER
  252. FOR i = 1 TO 1000
  253.   FOR j = 1 TO 1000
  254.     IF i < 0 AND j < 10 THEN x = 1
  255.   NEXT j
  256. NEXT i
  257. t!(26) = TIMER - t! - t!(0)
  258. t! = TIMER
  259. FOR i = 1 TO 1000
  260.   FOR j = 1 TO 1000
  261.     IF j < 10 AND i < 0 THEN x = 1
  262.   NEXT j
  263. NEXT i
  264. t!(27) = TIMER - t! - t!(0)
  265. 'Note: if the two times are appreciably different, some optimization has been
  266. 'done.  The first time should be shorter than the second.
  267.  
  268. 'screen output: print 1,000 70-byte strings
  269. x$ = STRING$(70, 66)
  270. t! = TIMER
  271. FOR i = 1 TO 1000
  272.   PRINT x$
  273. NEXT i
  274. t!(28) = TIMER - t! - t!(0) / 1000
  275.  
  276.  
  277. 'print results of benchmark
  278. PRINT #1, "Empty integer loop, 1,000,000 iterations:"; TAB(46); t!(0)
  279. PRINT #1, "1,000,000 integer assignments:"; TAB(46); t!(1)
  280. PRINT #1, "1,000,000 integer additions:"; TAB(46); t!(2)
  281. PRINT #1, "1,000,000 integer subtractions:"; TAB(46); t!(3)
  282. PRINT #1, "1,000,000 integer multiplications:"; TAB(46); t!(4)
  283. PRINT #1, "1,000,000 integer divisions:"; TAB(46); t!(5)
  284. PRINT #1, "100,000 string assignments:"; TAB(46); t!(6)
  285. PRINT #1, "100,000 string MID$ operations:"; TAB(46); t!(7)
  286. PRINT #1, "10,000 string concatenations:"; TAB(46); t!(8)
  287. PRINT #1, "1,000,000 single-precision assignments:"; TAB(46); t!(9)
  288. PRINT #1, "1,000,000 single-precision additions:"; TAB(46); t!(10)
  289. PRINT #1, "1,000,000 single-precision subtractions:"; TAB(46); t!(11)
  290. PRINT #1, "100,000 single-precision multiplications:"; TAB(46); t!(12)
  291. PRINT #1, "100,000 single-precision divisions:"; TAB(46); t!(13)
  292. PRINT #1, "Error in 100,000 single-precision mult/div:"; TAB(46); t!(14)
  293. PRINT #1, "10,000 single-precision exponentiations:"; TAB(46); t!(15)
  294. PRINT #1, "1,000,000 double-precision assignments:"; TAB(46); t!(16)
  295. PRINT #1, "1,000,000 double-precision additions:"; TAB(46); t!(17)
  296. PRINT #1, "1,000,000 double-precision subtractions:"; TAB(46); t!(18)
  297. PRINT #1, "100,000 double-precision multiplications:"; TAB(46); t!(19)
  298. PRINT #1, "100,000 double-precision divisions:"; TAB(46); t!(20)
  299. PRINT #1, "Error in 100,000 double-precision mult/div:"; TAB(46); t!(21)
  300. PRINT #1, "10,000 double-precision exponentiations:"; TAB(46); t!(22)
  301. PRINT #1, "1,000,000 integer comparisons:"; TAB(46); t!(23)
  302. PRINT #1, "1,000,000 single-precision comparisons:"; TAB(46); t!(24)
  303. PRINT #1, "1,000,000 double-precision comparisons:"; TAB(46); t!(25)
  304. PRINT #1, "1,000,000 conditional integer assignments:"; TAB(46); t!(26)
  305. PRINT #1, "1,000,000 conditional assignments (reversed):"; TAB(46); t!(27)
  306. PRINT #1, "Print 1,000 70-byte strings to the screen:"; TAB(46); t!(28)
  307. END
  308.  
  309.